package edu.thu.studio.ormdesigner.outline.editpart;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractTreeEditPart;

import edu.thu.studio.ormdesigner.model.AbstractModel;
import edu.thu.studio.ormdesigner.model.ORMModel;

public abstract class AbstractOutlineEditPart extends AbstractTreeEditPart implements PropertyChangeListener {
    
    /**
     * {@inheritDoc}
     */
    public void activate() {
        super.activate();
        ((AbstractModel) getModel()).addPropertyChangeListener(this);
    }
    
    /**
     * {@inheritDoc}
     */
    public void deactivate() {
        ((AbstractModel) getModel()).removePropertyChangeListener(this);
        super.deactivate();
    }
    
    public void propertyChange(PropertyChangeEvent event) {
        this.refreshOutline();
    }
    
    public void refreshOutline() {
        refreshChildren();
        
        refreshVisuals();
        
        for (Object child : this.getChildren()) {
            AbstractOutlineEditPart part = (AbstractOutlineEditPart) child;
            part.refreshOutline();
        }
    }
    
    @Override
    public void refresh() {
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    final public void refreshVisuals() {
        this.refreshOutlineVisuals();
    }
    
    protected ORMModel getORMModel() {
        return (ORMModel) this.getRoot().getContents().getModel();
    }
    
    abstract protected void refreshOutlineVisuals();
    
    protected void execute(Command command) {
        this.getViewer().getEditDomain().getCommandStack().execute(command);
    }
}
